const app = getApp();
let API_URL = app.globalData.apiurl;
var md5 = require("md5.js") 
var HeaderLogin = md5("csbrylpt&admincsbr").toUpperCase();
var requestHandler = {
  params: {},
  success: function (res) {
    // success  
  },
  fail: function () {
    // fail  
  },
}

//GET请求  
function GET(api_name, requestHandler) {
  request(api_name, 'GET', requestHandler)
}
//POST请求  
function POST(api_name, requestHandler) {
  request(api_name, 'POST', requestHandler)
}

function request(api_name, method, requestHandler) {
  //注意：可以对params加密等处理  
  var params = requestHandler.params;

  wx.request({
    url: API_URL+api_name,
    data: params,
    method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
    header: {
      // "Content-Type": "application/x-www-form-urlencoded" ,
      "Content-Type": "application/json;charset=UTF-8",
      "Authorization": "BasicAuth " + HeaderLogin
    },// 设置请求的 header  
    success: function (res) {
      //注意：可以对参数解密等处理  
      requestHandler.success(res)
    },
    fail: function (res) {
      requestHandler.fail()
    },
    complete: function () {
      // complete  
    }
  })
}

module.exports = {
  GET: GET,
  POST: POST
}  